package top.kaoshanji.p1ch09a;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 代码清单-p1ch09a-163页a：ImageProtectorFilter类
 * @author kaoshanji
 * ^_^
 * create time 2025/4/8 15:02
 */
@WebFilter(filterName = "ImageProtectorFilter", urlPatterns = {"*.png", "*.jpg", "*.gif"})
public class ImageProtectorFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("ImageProtectorFilter");

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String referrer = httpServletRequest.getHeader("referer");
        System.out.println("referrer: " + referrer);
        if (referrer != null) {
            chain.doFilter(request, response);
        } else {
            throw new ServletException("Image not available");
        }
    }


}
